﻿using System;
using System.Diagnostics;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Clarendon.AzureBilling.Library;

namespace Clarendon.AzureBilling.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class TestServiceList
    {
        private readonly ServiceManager _manager = null;
        private readonly Semaphore _semaphore = null;

        public TestServiceList()
        {
            _semaphore = new Semaphore(0, 1);
            _manager = new ServiceManager(_semaphore);
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        /* Use ClassInitialize to run code before running the first test in the class
         * TODO: Inevitably we want a clean account here - take a default deployment package and
         * upload using the Create Deployment API */
        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext)
        {
        }
        
         /*Use ClassCleanup to run code after all tests in a class have run
          * TODO: We need to use this to teardown any deployment so that the test can be self container*/
         [ClassCleanup()]
         public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestListHostedServices()
        {
            ServiceListRequester request = new ServiceListRequester(_manager);
            request.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsNotNull(_manager.HostedServices);
            Assert.IsTrue(_manager.HostedServices.Count > 0);
        }

        [TestMethod, ExpectedException(typeof(AzureServicesException))]
        public void TestOrderOfPrecedenceForHostedServiceProperties()
        {
            ServicePropertiesRequester properties = new ServicePropertiesRequester(_manager);
            properties.MakeServiceRequest();
            _semaphore.WaitOne();
        }

        [TestMethod]
        public void TestHostedServiceProperties()
        {
            ServiceListRequester request = new ServiceListRequester(_manager);    
            request.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsTrue(_manager.HostedServices.Count > 0);

            ServicePropertiesRequester properties = new ServicePropertiesRequester(_manager);
            properties.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsTrue(_manager.HostedServices.Count > 0);
            Assert.IsNotNull(_manager.HostedServices.FirstOrDefault().Location);
        }

        [TestMethod, ExpectedException(typeof(AzureServicesException))]
        public void TestDeploymentsOrderDependence()
        {
            ServiceListRequester request = new ServiceListRequester(_manager);
            request.MakeServiceRequest();
            _semaphore.WaitOne();

            DeploymentsRequester requester = new DeploymentsRequester(_manager);
            requester.MakeServiceRequest();
            _semaphore.WaitOne();
        }

        [TestMethod]
        public void TestHostedServiceInstances()
        {
            ServiceListRequester request = new ServiceListRequester(_manager);
            request.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsTrue(_manager.HostedServices.Count > 0);

            ServicePropertiesRequester properties = new ServicePropertiesRequester(_manager);
            properties.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsTrue(_manager.HostedServices.Count > 0);
            Assert.IsNotNull(_manager.HostedServices.FirstOrDefault().Location);

            DeploymentsRequester requester = new DeploymentsRequester(_manager);
            requester.MakeServiceRequest();
            _semaphore.WaitOne();
            Assert.IsNotNull(_manager.HostedServices[0].Deployments);
            Assert.IsNotNull(_manager.HostedServices[0].Deployments[0].RoleInstanceList);
        }
       
    }
}
